package com.example.springbootwebsocketmonikehuduan;


import com.alibaba.fastjson.JSON;
import com.example.springbootwebsocketmonikehuduan.entity.Menjing;
import org.apache.log4j.Logger;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;




import java.net.URI;
import java.net.URISyntaxException;


/**
 * @program: springboot-websocket-monikehuduan
 * @description: 22
 * @author: XianJin.Lu
 * @create: 2021-09-02 16:47
 **/
public class WebsocketClient222 {

    private static Logger logger = Logger.getLogger(WebsocketClient222.class);
    public static WebSocketClient client;
    public static void main(String[] args) {
        try {
            //测试用  ws://113.116.35.114:55551        现场    ws://10.0.7.70:9008
            client = new WebSocketClient(new URI("ws://10.0.7.70:9008"),new Draft_6455()) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                  logger.info("连接成功");

                }

                @Override
                public void onMessage(String message) {

               logger.info("收到消息=========="+message);


               // Teacher teacher = JSON.parseObject(message, Teacher.class);
               // System.out.println("转换为teacher实体类==========" + teacher.toString());

               //Menjing response00 = JSON.parseObject(message, Menjing.class);
               //System.out.println(response00.toString());

                    if(message.equals("over")){
                        client.close();
                    }

                }

                @Override
                public void onClose(int i, String s, boolean b) {
                   logger.info("链接已关闭");
                }

                @Override
                public void onError(Exception e){
                    e.printStackTrace();
                 logger.info("发生错误已关闭");
                }
            };
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        client.connect();
        logger.info(client.getDraft());
        while(!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
          logger.info("正在连接...");

        }

        String s3= "{\"head\" : { \"cmdType\":1, \"cmdSn\":2222: } ,\"data\": { \"devId\":0 } }";
        String s2= "{head : { \"cmdType\":2, \"cmdSn\":2222: } ,data : { \"devId\":0,\"operType\":0,\"doorIndex\":1 } }";
        String s= "{\"head\" : { \"cmdType\":1, \"cmdSn\":\"13c1d6ba578142b0a18fbab1ac856b50\": } ,\"data\" : { \"devId\":0 } }";


        //测试请求体
       String   s4=   "{'head':{'cmdType':1,'cmdSn':'13c1d6ba578142b0a18fbab1ac856b50'},'data':{'devId':0}}";
        //连接成功,发送信息
        client.send(s4);

    }

}

